package com.dh.blog.test.utils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrlPattern;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
/**
 * 测试工具类
 * @author David
 *
 */
public class MockMvcHttp {
	private static final String DEFAULT_ENCODING = "UTF-8";
	public static void mockJson(MockMvc mockMvc,String uri,
				String type,String json) throws Exception{
		MockHttpServletRequestBuilder builder = checkType(uri,type);
		if(json == null){
			json = "";
		}
		if(null != builder){
			mockMvc.perform(builder.characterEncoding(DEFAULT_ENCODING)
					.contentType(MediaType.APPLICATION_JSON)
					.content(json))
					.andDo(print())
					.andExpect(status().isOk());
		}
	}
	public static void mockParams(MockMvc mockMvc,String uri,
			String type,Map<String,String> params) throws Exception{
		MockHttpServletRequestBuilder builder = checkType(uri,type);
		if(null != builder){
			if(null != params && params.size() > 0){
				for(Entry<String,String> entry : params.entrySet()){
					builder = builder.param(entry.getKey(),entry.getValue());
				}
			}
			mockMvc.perform(builder.characterEncoding(DEFAULT_ENCODING)
					.contentType(MediaType.ALL_VALUE))
					.andExpect(status().isOk())
					.andDo(print());
		}
	}
	public static void mockPages(MockMvc mockMvc,String uri,String forward) throws Exception{
		mockMvc.perform(get(uri))
			.andExpect(status().isOk())
			.andExpect(forwardedUrlPattern(forward)).andDo(print());
	}
	public static void mockFile(MockMvc mockMvc,String uri,String name,String path) throws Exception{
		InputStream in = new FileInputStream(path);
		MockMultipartFile mockFile = new MockMultipartFile(name, 
				in);
		mockMvc.perform(fileUpload(uri).file(mockFile)
				.contentType(MediaType.MULTIPART_FORM_DATA))
				.andExpect(status().isOk())
				.andDo(print());
	}
	private static MockHttpServletRequestBuilder 
			checkType(String uri,String type){
		MockHttpServletRequestBuilder builder = null;
		if("post".equals(type)){
			builder = post(uri);
		}else if("get".equals(type)){
			builder = get(uri);
		}else if("put".equals(type)){
			builder = put(uri);
		}else if("delete".equals(type)){
			builder = delete(uri);
		}
		return builder;
	}
}
